package com.huang825172.sbog.Tabs.SoundAndExplore.VideoPage.View;

import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.huang825172.sbog.R;
import com.huang825172.sbog.Tabs.SoundAndExplore.VideoPage.Action.ActionCreator;
import com.huang825172.sbog.Tabs.SoundAndExplore.VideoPage.Store.Datas.getVideoInfoRes;
import com.huang825172.sbog.Util.TAGs;
import com.huang825172.sbog.Util.VideoFileCacheManager;

import org.w3c.dom.Text;

import java.net.URI;
import java.util.ArrayList;

import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;
import de.hdodenhof.circleimageview.CircleImageView;
import me.rosuh.filepicker.config.FilePickerManager;

public class VideoPageViewPagerAdapter extends PagerAdapter {

    private ArrayList<View> viewLists;
    private ArrayList<VideoFileCacheManager.VideoItem> dataSet;
    private getVideoInfoRes info;
    private boolean infoIsSet = false;
    private boolean firstInit = true;
    private ActionCreator actionCreator;
    private String type = "music";

    public VideoPageViewPagerAdapter(ArrayList<View> viewLists, ArrayList<VideoFileCacheManager.VideoItem> dataSet) {
        super();
        this.viewLists = viewLists;
        this.dataSet = dataSet;
    }

    @Override
    public int getCount() {
        return viewLists.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View obj = viewLists.get(position);
        container.addView(obj);

        if (position == 0 && firstInit){
            actionCreator = new ActionCreator();
            actionCreator.createGetVideoInfoAction(0);
            firstInit = false;
        }

        JzvdStd player = (JzvdStd)obj.findViewById(R.id.VideoPageVideoPlayer);
        CircleImageView authorPhoto = (CircleImageView)obj.findViewById(R.id.VideoPageAuthorPhotoImageView);
        TextView authorID = (TextView)obj.findViewById(R.id.VideoPageAuthorIDTextView);
        TextView authorInfoID = (TextView)obj.findViewById(R.id.VideoPageAuthorInfoIDTextView);
        TextView topic = (TextView)obj.findViewById(R.id.VideoPageTopicTextView);
        TextView discription = (TextView)obj.findViewById(R.id.VideoPageVideoDiscriptionTextView);
        CircleImageView musicCover = (CircleImageView)obj.findViewById(R.id.VideoPageMusicCoverImageView);

        RelativeLayout plane = (RelativeLayout)obj.findViewById(R.id.VideoPageAlbumPlaneContainer);
        ImageView pin = (ImageView)obj.findViewById(R.id.VideoPageAlbumPin);
        LinearLayout shop = (LinearLayout)obj.findViewById(R.id.VideoPageShopEntrence);

        if (type.equals("buy")){
            plane.setVisibility(View.INVISIBLE);
            pin.setVisibility(View.INVISIBLE);
            shop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    actionCreator.createOpenShopAction();
                }
            });
        } else {
            shop.setVisibility(View.INVISIBLE);
        }

        if(dataSet.size()==0){
            return obj;
        }
        int id = 0;
        String url = "";
        if(dataSet.size() >= position+1){
            url = dataSet.get(position).url;
            Log.d(TAGs.INFO, "Setting Up Pager " + String.valueOf(position) + " with url: " + url);
            player.thumbImageView.setImageResource(R.mipmap.video_loading);
            if(infoIsSet){
                authorID.setText(info.data.phone);
                authorInfoID.setText(info.data.phone);
                topic.setText(info.data.text);
                discription.setText(info.data.tag);
                authorPhoto.setImageResource(R.mipmap.login_background);
                musicCover.setImageResource(R.mipmap.register_background);
            }
        } else {
            Log.d(TAGs.INFO, "Setting Up Pager " + String.valueOf(position) + " with No Video");
            player.thumbImageView.setImageResource(R.mipmap.video_nomore);
        }
        player.setUp(url, "",JzvdStd.SCREEN_WINDOW_FULLSCREEN|JzvdStd.SCREEN_STATE_OFF);

        return obj;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(viewLists.get(position));
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    public void UpdateData(ArrayList<VideoFileCacheManager.VideoItem> dataSet){
        this.dataSet = dataSet;
    }

    public void UpdataInfo(getVideoInfoRes infoRes){
        this.info = infoRes;
        if(infoRes != null) {
            this.infoIsSet = true;
        }
    }

    public void setType(String type){
        this.type = type;
    }
}
